package com.mec.taskassign.utils;

import com.alibaba.fastjson.JSONObject;

public class ResponseResult extends JSONObject {
    private static final int SUCCESS_CODE=200;
    private static final int ERROR_CODE=500;
    private static final int LAYUI_SUCCESS_CODE=0;

    private static final String CODE_KEY="code";
    private static final String MSG_KEY="msg";
    private static final String DATA_KEY="data";
    private static final String COUNT_KEY="count";


    private ResponseResult(){

    }

    //通过静态方法获取实例
    private static ResponseResult getInstance(){
        return new ResponseResult();
    }

    //定义put方法并且每次调用完就返回自身
    public ResponseResult set(String key,Object object){
        super.put(key,object);
        return this;
    }

    //调用成功五种返回形式
    public static ResponseResult success(){
        return ResponseResult.getInstance()
                .set(CODE_KEY,SUCCESS_CODE)
                .set(MSG_KEY,"Data retrieving success");
    }
    public static ResponseResult success(String msg){
        return ResponseResult.getInstance()
                .set(CODE_KEY,SUCCESS_CODE)
                .set(MSG_KEY,msg);
    }
    public static ResponseResult success(String msg,Object object){
        return ResponseResult.getInstance()
                .set(CODE_KEY,SUCCESS_CODE)
                .set(MSG_KEY,msg)
                .set(DATA_KEY,object);
    }
    public static ResponseResult success(String msg,Object object,int count){
        return ResponseResult.getInstance()
                .set(CODE_KEY,SUCCESS_CODE)
                .set(MSG_KEY,msg)
                .set(DATA_KEY,object)
                .set(COUNT_KEY,count);
    }
    //layui要求code=0，msg=""，同时有data，count才可以
    public static ResponseResult success(String msg,Object object,int count,int code){
        return ResponseResult.getInstance()
                .set(CODE_KEY,code)
                .set(MSG_KEY,msg)
                .set(DATA_KEY,object)
                .set(COUNT_KEY,count);
    }


    //调用成功四种返回形式
    public static ResponseResult error(){
        return ResponseResult.getInstance()
                .set(CODE_KEY,ERROR_CODE)
                .set(MSG_KEY,"Data retrieving failed");
    }
    public static ResponseResult error(String msg){
        return ResponseResult.getInstance()
                .set(CODE_KEY,ERROR_CODE)
                .set(MSG_KEY,msg);
    }
    public static ResponseResult error(String msg,Object object){
        return ResponseResult.getInstance()
                .set(CODE_KEY,ERROR_CODE)
                .set(MSG_KEY,msg)
                .set(DATA_KEY,object);
    }
    public static ResponseResult error(String msg,Object object,int count){
        return ResponseResult.getInstance()
                .set(CODE_KEY,ERROR_CODE)
                .set(MSG_KEY,msg)
                .set(DATA_KEY,object)
                .set(COUNT_KEY,count);
    }
    public static ResponseResult error(String msg,Object object,int count,int code){
        return ResponseResult.getInstance()
                .set(CODE_KEY,code)
                .set(MSG_KEY,msg)
                .set(DATA_KEY,object)
                .set(COUNT_KEY,count);
    }

}
